package cn.web;

import cn.web.entity.Student;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.convert.converter.Converter;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

@RestController
public class UserController {
    static Logger logger = LoggerFactory.getLogger(UserController.class);

@RequestMapping("/test3")
    public String  test(){
        System.out.println("test方法被调用");
        logger.info("test方法被调用");
        return "index";
    }
    @RequestMapping("/test4")
    @ResponseBody
    public void test4(){
        System.out.println("test4方法被调用");
        logger.info("test4方法被调用");
    }
    @RequestMapping("/test5")
    public ModelAndView test5(){
        System.out.println("test5方法被调用");
        logger.info("test5方法被调用");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        modelAndView.setStatus(HttpStatus.OK);
        return modelAndView;
    }
    @RequestMapping("/test6")
    public String test6(){
        System.out.println("test6方法被调用");
        logger.info("test6方法被调用");
        return "redirect:/index.jsp";
    }
    @RequestMapping("/test7")
    public void test7(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("test7方法被调用");
        logger.info("test7方法被调用");
        System.out.println(request);
        System.out.println(response);

        request.getRequestDispatcher("/test").forward(request,response);
    }
     @RequestMapping("/insert")
    public String insert(@RequestParam(name ="sidd",required = true,defaultValue = "1")
                         String sid ,String name,String address){
         System.out.println("insert方法被调用");
         System.out.println("sid"+sid);
         System.out.println("name"+name);
         System.out.println("address"+address);
           return "index";
     }
     @RequestMapping("/delete")
    public String delete(String[] sid){
         System.out.println("delete方法被调用");
         System.out.println(Arrays.toString(sid));
         return "index";
     }
     @RequestMapping("/update")
    public  String update(Student s){
         System.out.println("update方法被调用");
         System.out.println(s);
         return "index";
     }
     public class DateConver implements Converter<String, Date>{

         @Override
         public Date convert(String s) {
             SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
             Date parse = null;
             try {
                 parse = format.parse(s);
                 return parse;
             } catch (ParseException e) {
                 e.printStackTrace();
             }

             return null;
         }
     }
}
